package java_05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class doudizhu {
	public static void main(String[] args) {
		ArrayList<String> poker = new ArrayList<String>();
		String[] colors = {"♠", "♣", "♦", "♥"};
		String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
		poker.add("大王");
		poker.add("小王");
		for (String number : numbers) {
			for (String color : colors) {
				poker.add(color + number);
			}
		}
		System.out.println(poker);
		Collections.shuffle(poker);
		System.out.println(poker);
		Player p1 = new Player(); p1.setName("老王");
		Player p2 = new Player(); p2.setName("老李");
		Player p3 = new Player(); p3.setName("老刘");
		ArrayList<String> dipai = new ArrayList<String>();
		for (int i = 0; i < poker.size(); i++) {
			String p = poker.get(i);
			if (i >= 51) {
				dipai.add(p);
			} else if (i % 3 == 0) {
				p1.getPoker().add(p);
			} else if (i % 3 == 1) {
				p2.getPoker().add(p);
			} else if (i % 3 == 2) {
				p3.getPoker().add(p);
			}
		}
		sortByNumber(p1.getPoker());
		sortByNumber(p2.getPoker());
		sortByNumber(p3.getPoker());
		System.out.println(p1.getName() + "的牌:" + p1.getPoker());
		System.out.println(p2.getName() + "的牌:" + p2.getPoker());
		System.out.println(p3.getName() + "的牌:" + p3.getPoker());
		System.out.println("底牌:" + dipai);
	}
	private static void sortByNumber(List<String> hand) {
		HashMap<String, Integer> orderMap = new HashMap<>();
		String[] order = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
		for (int i = 0; i < order.length; i++) {
			orderMap.put(order[i], i);
		}
		Collections.sort(hand, (card1, card2) -> {
			if (card1.equals("大王") && card2.equals("大王")) return -1;
			if (card1.equals("小王") && card2.equals("小王")) return -1;
			if (card1.equals("大王")) return -1;
			if (card2.equals("大王")) return 1;
			if (card1.equals("小王")) return -1;
			if (card2.equals("小王")) return 1;
			String number1 = card1.substring(1); 
			String number2 = card2.substring(1);
			int index1 = orderMap.get(number1);
			int index2 = orderMap.get(number2);

			return Integer.compare(index1, index2);
		});
	}
}
